﻿using BCDto.Common.ErrorMessage;
using BCDto.TMS.FreightAddress;
using BCService.TMS.FreightAddressService;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BCTMSRESTful.Controllers.ClientApp.Freight
{
    /// <summary>
    /// 用户货运常用地址
    /// </summary>
    [ApiExplorerSettings(GroupName = "ClientApp")]
    [Route("api/v1/ClientApp/[controller]")]
    public class FreightAddressController : ClientAppBaseController
    {
        private readonly IFreightAddressService freightAddressService;

        public FreightAddressController(IFreightAddressService freightAddressService)
        {
            this.freightAddressService = freightAddressService;
        }

        /// <summary>
        /// 新增用户货运地址
        /// </summary>
        /// <param name="requestDto"></param>
        /// <response code="200">创建车辆成功。</response>
        /// <response code="400">请求参数错误。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("新增用户货运地址")]
        [ProducesResponseType(typeof(FreightAddressDto), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 401)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult Post([FromBody] FreightAddressRequestDto requestDto)
        {
            var result = this.freightAddressService.Add(this.UserInfo, requestDto);
            return Created("api/v1/ClientApp/FreightAddress/{0}", result.FreightAddressId);
        }

        /// <summary>
        /// 修改用户货运地址
        /// </summary>
        /// <param name="freightAddressId">货运地址ID</param>
        /// <param name="requestDto"></param>
        /// <response code="201">数据修改成功。</response>
        /// <response code="400">请求参数错误。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <returns></returns>
        [HttpPut("{freight_address_id}")]
        [Log("修改用户货运地址")]
        [ProducesResponseType(typeof(OkObjectResult), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 401)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult Put([FromRoute(Name = "freight_address_id")] long freightAddressId, [FromBody] FreightAddressRequestDto requestDto)
        {
            return Ok(freightAddressService.Update(freightAddressId, requestDto));
        }

        /// <summary>
        /// 修改货运地址使用次数
        /// </summary>
        /// <param name="freightAddressId">货运地址ID</param>
        /// <response code="201">数据修改成功。</response>
        /// <response code="400">请求参数错误。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <returns></returns>
        [HttpPut("[action]/{freight_address_id}")]
        [Log("修改货运地址使用次数")]
        [ProducesResponseType(typeof(OkObjectResult), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 401)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult UserCount([FromRoute(Name = "freight_address_id")] long freightAddressId)
        {
            return Ok(freightAddressService.UpdateUserCount(freightAddressId));
        }

        /// <summary>
        /// 用户货运地址列表
        /// </summary>
        /// <response code="200">数据查询成功。</response>
        /// <response code="400">请求参数错误。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(List<FreightAddressDto>), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 401)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult UserFreightList()
        {
            return Ok(this.freightAddressService.GetListByUserId(this.UserInfo.UserId));
        }

        /// <summary>
        /// 获取指定的货运地址
        /// </summary>
        /// <param name="freightAddressId">货运地址id</param>
        /// <response code="200">数据查询成功。</response>
        /// <response code="400">请求参数错误。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <returns></returns>
        [HttpGet("{freight_address_id}")]
        [ProducesResponseType(typeof(FreightAddressDto), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 401)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult GetById([FromRoute(Name = "freight_address_id")] long freightAddressId)
        {
            var res = freightAddressService.GetById(freightAddressId);
            return Ok(res);
        }
    }
}
